package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.AddReportService;
import com.yixun.qfbao.apidto.AddBatchCusPreReqDto;
import com.yixun.qfbao.apidto.AddBatchCusPreResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.*;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AddBatchCusPreReqVo;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.model.enums.SaleStatusEnum;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import com.yixun.qfbao.xf.service.PremisesService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 批量添加报备
 * @author jhsong
 *
 */
@Service("addBatchCusPreHander")
public class AddBatchCusPreHander extends CommonAbstractHander {

	private static final Logger logger = LoggerFactory.getLogger(AddBatchCusPreHander.class);

	@Autowired
	private LoginUtils loginUtils;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private PremisesService premisesService;
	@Autowired
	private CustomerPremisesService customerPremisesService;
	@Autowired
	AddReportService addReportService;

	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		AddBatchCusPreResDto resDto = new AddBatchCusPreResDto();
		BaseResponseDTO<AddBatchCusPreResDto> res = new BaseResponseDTO<>();
		AddBatchCusPreReqVo reqVo = (AddBatchCusPreReqVo) reqDto;
		AddBatchCusPreReqDto addBatchCusPreReqDto = reqVo.getParams();
		try {
			if(null == addBatchCusPreReqDto.getUserId()){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("登录超时,无法导入,请登录后重试!");
				return res;
			}
			//获取经纪人信息
			PartnerInfo pi = loginUtils.getUser(addBatchCusPreReqDto.getUserId());
			if(null == pi){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("登录超时,无法导入,请登录后重试!");
				return res;
			}

			//根据楼盘获取开发商信息
			//1、校验楼盘是否存在
			//2、校验楼盘是否可报备
			String premisesId = addBatchCusPreReqDto.getPremisesId();
			if(StringUtils.isBlank(premisesId)){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("楼盘不存在!");
				return res;
			}

			Premises premises = premisesService.getSimpleById(Long.valueOf(premisesId));
			if(null == premises){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("楼盘不存在!");
				return res;
			}

			if(DelFlagEnum.DEL.getNo().equals(premises.getDelFlag())){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("楼盘已删除，无法报备!!");
				return res;
			}

			if(SaleStatusEnum.type_3.equals(premises.getSaleStatus()) ){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("楼盘已下架，无法报备!");
				return res;
			}
			//检验楼盘代理商是否存在
			Company company = companyService.selectById(premises.getCompanyId());
			if(null == company){
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("楼盘信息错误,对应代理商未找到!");
				return res;
			}

			String repeatReportSwitch = company.getRepeatReportSwitch();
			boolean repeatSwitch = false;
			if("1".equals(repeatReportSwitch)){
				if(new Integer(1).equals(premises.getRepeatReportFlag())){
					repeatSwitch = true;
				}
			}

			String batchId = addBatchCusPreReqDto.getBatchId();
			String visitProtectDate = addBatchCusPreReqDto.getVisitProtectDate();
			String remark = addBatchCusPreReqDto.getRemark();
			String recommenderUuid = addBatchCusPreReqDto.getRecommenderUuid();
			Map<String, String> addBatchMap = new HashMap<>();
			addBatchMap.put("batchId", batchId);
			addBatchMap.put("visitProtectDate", visitProtectDate);
			addBatchMap.put("remark", remark);
			addBatchMap.put("agentCompany",String.valueOf(pi.getHigherid()));
			addBatchMap.put("recommenderUuid", recommenderUuid);


			long startTime = System.currentTimeMillis();
			logger.info("add batch start, batchId = " + batchId + ",start time = " + startTime);
			Map<String, Object> returnMap = addReportService.addBatchCusPre(addBatchMap, repeatSwitch, premises,addBatchCusPreReqDto.getUserId());
			long endTime = System.currentTimeMillis();
			logger.info("add batch end, batchId = " + batchId + ",end time = " + endTime);
			logger.info("add batch batchId = " + batchId + ", use time = " + (endTime - startTime));

			res.setResult((String) returnMap.get("result"));
			res.setErrorMsg((String) returnMap.get("errorMsg"));
			res.setResult(ResultEnum.SUCCESS.getNo());
			res.setErrorMsg("执行成功");
			//判断是否存在错误数据
			resDto.setHasErrorRecord(returnMap.get("hasErrorRecord")==null?false: (Boolean) returnMap.get("hasErrorRecord"));
			resDto.setShowMsg((String) returnMap.get("showMsg"));
			res.setResData(resDto);
			return res;
		} catch (Exception e) {
			logger.error("AddBatchCusPreHander error: {}", e);
			res.setResult(ResultEnum.FAIL.getNo());
			res.setErrorMsg("导入报备失败");
		}
		return res;
	}
	
}
